/**
 * 签到
 */
$(function () {
    $("#page").load(URI + "/rbac/html/page.html");
});

//查询所有数据
function findAll() {
    //trim()去掉两边的空格
    let data = {
        username: $("#username").val(),
        sno: $("#sno").val(),
        nickName: $("#nickName").val(),
        phone: $("#phone").val(),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val()
    };
    //查询当前用户的角色，如果时组长则返回当前小组的小组成员
    let aa = myAjax("/api/rb/urr/")
    let result = myAjax("/api/rb/user/getStudent", data, "post");
    // console.log(resultList);
    // let  result = adminLogin(resultList);
    // 总条数
    if (result) {
        $("#totalCount").html(result.data.count);
        setData(result.data.pageInfo.list);
    }
}

//遍历查询到的数据
function setData(data) {
    let html = "";
    for (let i = 0; i < data.length; i++) {
        html += " <tr >\n" +
            "            <td>" + data[i].id + "</td>\n" +
            "            <td title='" + data[i].nickName + "'>" + clearNull(data[i].nickName) + "</td>\n" +
            // "            <td title='" + data[i].username + "'>" + clearNull(data[i].username) + "</td>\n" +
            "            <td title='" + data[i].icon + "'><img src='" + clearNull(URI + IMG + data[i].icon) + "' style='width: 30px; height: 30px;' ></td>\n" +
            "            <td title='" + data[i].sex + "'>" + clearNull(data[i].sex) + "</td>\n" +
            // "            <td title='" + data[i].age + "'>" + clearNull(data[i].age) + "</td>\n" +
            "            <td title='" + data[i].phone + "'>" + clearNull(data[i].phone) + "</td>\n" +
            "            <td title='" + data[i].email + "'>" + clearNull(data[i].email) + "</td>\n" +
            "            <td title='" + data[i].sno + "'>" + clearNull(data[i].sno) + "</td>\n" +
            "            <td title='" + data[i].className + "'>" + clearNull(data[i].className) + "</td>\n" +
            "            <td title=''>" +
            "               <input type=\"radio\" name=\"status\" value=\"已到\" onclick='yidao(" + data[i].id + ")'>已到\n" +
            "               <input type=\"radio\" name=\"status\" value=\"迟到\" onclick='chidao(" + data[i].id + ")'>迟到\n" +
            "               <input type=\"radio\" name=\"status\" value=\"请假\" onclick='yidao(" + data[i].id + ")' >请假\n" +
            "               <input type=\"radio\" name=\"status\" value=\"旷课\" onclick='yidao(" + data[i].id + ")'>旷课\n" +
            "            </td>\n" +
            "            <td title=''>" +
            "                  <input type=\"date\" id='signLateTime" + data[i].id + "' class=\"layui-input\" style='display:none;'>\n" +
            "            </td>\n" +
            "            <td class=\"td-manage\" style='text-align: center;'>\n" +
            "                  <div id='sign' style='background-color: #00FF00;width: 40px;height: 40px; border-radius: 50%;display: flex;align-items:center ;justify-content: center;font-size: larger;'>" +
            "                      <i class=\"layui-icon layui-icon-close\" id='icon" + data[i].id + "' onclick=\"sign(" + data[i].id + ")\" style='color: blue;font-size: 18px; font-weight: bolder;'></i>\n" +
            "                  </div>  " +
            "            </td>\n" +
            "          </tr>";
    }
    $("#tbody").html(html);
}

/***
 * 清除数据中的undefined和null将其置换为空，不显示
 */

function clearNull(value) {
    return value === "null" || value === undefined ? "" : value;
}


/***
 * 删除功能
 */
function del(id) {
    let result = myAjax("/api/rb/user/del", {id: id}, "delete");
    if (result.code === 200) {
        //删除成功，就重新查询数据
        alert("删除成功(== ^^ ==)!");
        findAll();
    } else {
        alert("删除失败(╥╯^╰╥)！")
    }
}


/**
 * 签到
 */
// function sign(id) {
//     sessionStorage.setItem("newUserId",id);
//     x_admin_show('编辑', URI + '/rbac/html/student/sign-update.html', 600, 600);
// }
function yidao(id) {
    let status = $("input:radio[name='status']:checked").val();
    if(status === '已到'){
        $("#signLateTime" + id).attr("style", "display:none;");
    }
}
function chidao(id){
    let status = $("input:radio[name='status']:checked").val();
    if(status === '迟到'){
        $("#signLateTime" + id).attr("style", "");
    }
}
function sign(id) {
    let status = $("input:radio[name='status']:checked").val();

    let data = {
        userId: id,
        status: status,
        lateTime: $("#signLateTime" + id).val()
    };
    let result = myAjax("/api/stu/student/sign", data, "post");

    if (result.code === 200) {
        //签到成功
        alert(result.message);
        $("#icon" + id).attr("class", "layui-icon layui-icon-ok");
    } else {
        layer.alert(result.message, {icon: 2});
    }
}
